### R code from vignette source 'article2.Rnw'
### Encoding: UTF-8

###################################################
### code chunk number 1: load
###################################################
library(reshape)
library(car)
library(Hmisc)
library(xtable)
library(memisc)
library(MCMCpack)
library(pscl)
library(ggplot2)
library(rio)
library(countrycode)

set.seed(1982)

strength.text<-function(x) {
	if (sign(x)>0) {
		sign.chunk<-" and positive"
	}	else {
		sign.chunk<-" and negative"
	}
	if (abs(x)<0.2) {
		strength.chunk<-"negligible"
	} else if (abs(x)<0.3) {
		strength.chunk<-"weak"
	} else if (abs(x)<0.4) {
		strength.chunk<-"moderate"
	} else if (abs(x)<0.7) {
		strength.chunk<-"strong"
	} else if (abs(x)>0.7) {
		strength.chunk<-"very strong"
	}
	return(paste(strength.chunk,sign.chunk,sep=""))
}


sanitize.text<-function(x) {
	x<-gsub("Á","A",x)
	x<-gsub("Ň","N",x)
	x<-gsub("Ž","Z",x)
	x<-gsub("Ů","U",x)
	x<-gsub("Ý","Y",x)
	x<-gsub("Č","C",x)
	x<-gsub("Ř","R",x)
	x<-gsub("Í","I",x)
	x<-gsub("Š","S",x)
	x<-gsub("í","i",x)
	x<-gsub("š","s",x)
	x<-gsub("ř","r",x)
	x<-gsub("ě","e",x)
	x<-gsub("Ľ","L",x)
	x<-gsub("ň","n",x)
	x<-gsub("č","c",x)
	x<-gsub("š","s",x)
	x<-gsub("ă","a",x)
	x<-gsub("ž","z",x)
	x
}

venice.string<-"'1'='Constitutional Justice';
'1.1'='Constitutional jurisdiction';
'1.1.1'='Statute and organisation';
'1.1.1.1'='Sources';
'1.1.1.1.1'='Constitution';
'1.1.1.1.2'='Institutional Acts';
'1.1.1.1.3'='Other legislation';
'1.1.1.1.4'='Rule issued by the executive';
'1.1.1.1.5'='Rule adopted by the Court';
'1.1.1.2'='Independence';
'1.1.1.2.1'='Statutory independence';
'1.1.1.2.2'='Administrative independence';
'1.1.1.2.3'='Financial independence';
'1.1.2'='Composition, recruitment and structure';
'1.1.2.1'='Necessary qualifications';
'1.1.2.2'='Number of members';
'1.1.2.3'='Appointing authority';
'1.1.2.4'='Appointment of members';
'1.1.2.5'='Appointment of the President';
'1.1.2.6'='Functions of the President / Vice-President';
'1.1.2.7'='Subdivision into chambers or sections';
'1.1.2.8'='Relative position of members';
'1.1.2.9'='Persons responsible for preparing cases for hearing';
'1.1.2.10'='Staff';
'1.1.2.10.1'='Functions of the Secretary General / Registrar';
'1.1.2.10.2'='Legal Advisers';
'1.1.3'='Status of the members of the court';
'1.1.3.1'='Term of office of Members';
'1.1.3.2'='Term of office of the President';
'1.1.3.3'='Privileges and immunities';
'1.1.3.4'='Professional incompatibilities';
'1.1.3.5'='Disciplinary measures';
'1.1.3.6'='Remuneration';
'1.1.3.7'='Non-disciplinary suspension of functions';
'1.1.3.8'='End of office';
'1.1.3.9'='Members having a particular status';
'1.1.3.10'='Status of staff';
'1.1.4'='Relations with other institutions';
'1.1.4.1'='Head of State';
'1.1.4.2'='Legislative bodies';
'1.1.4.3'='Executive bodies';
'1.1.4.4'='Courts';
'1.2'='Types of claim';
'1.2.1'='Claim by a public body';
'1.2.1.1'='Head of State';
'1.2.1.2'='Legislative bodies';
'1.2.1.3'='Executive bodies';
'1.2.1.4'='Organs of federated or regional authorities';
'1.2.1.5'='Organs of sectoral decentralisation';
'1.2.1.6'='Local self-government body';
'1.2.1.7'='Public Prosecutor or Attorney-General';
'1.2.1.8'='Ombudsman';
'1.2.1.9'='Member states of the European Union';
'1.2.1.10'='Institutions of the European Union';
'1.2.1.11'='Religious authorities';
'1.2.2'='Claim by a private body or individual';
'1.2.2.1'='Natural person';
'1.2.2.2'='Non-profit-making corporate body';
'1.2.2.3'='Profit-making corporate body';
'1.2.2.4'='Political parties';
'1.2.2.5'='Trade unions';
'1.2.3'='Referral by a court';
'1.2.4'='Initiation ex officio by the body of constitutional jurisdiction';
'1.2.5'='Obligatory review';
'1.3'='Jurisdiction';
'1.3.1'='Scope of review';
'1.3.1.1'='Extension';
'1.3.2'='Type of review';
'1.3.2.1'='Preliminary / ex post facto review';
'1.3.2.2'='Abstract / concrete review';
'1.3.3'='Advisory powers';
'1.3.4'='Types of litigation';
'1.3.4.1'='Litigation in respect of fundamental rights and freedoms';
'1.3.4.2'='Distribution of powers between State authorities';
'1.3.4.3'='Distribution of powers between central government and federal or regional entities';
'1.3.4.4'='Powers of local authorities';
'1.3.4.5'='Electoral disputes';
'1.3.4.6'='Litigation in respect of referendums and other instruments of direct democracy';
'1.3.4.6.1'='Admissibility';
'1.3.4.6.2'='Other litigation';
'1.3.4.7'='Restrictive proceedings';
'1.3.4.7.1'='Banning of political parties';
'1.3.4.7.2'='Withdrawal of civil rights';
'1.3.4.7.3'='Removal from parliamentary office';
'1.3.4.7.4'='Impeachment';
'1.3.4.8'='Litigation in respect of jurisdictional conflict';
'1.3.4.9'='Litigation in respect of the formal validity of enactments';
'1.3.4.10'='Litigation in respect of the constitutionality of enactments';
'1.3.4.10.1'='Limits of the legislative competence';
'1.3.4.11'='Litigation in respect of constitutional revision';
'1.3.4.12'='Conflict of laws';
'1.3.4.13'='Universally binding interpretation of laws';
'1.3.4.14'='Distribution of powers between the EU and member states';
'1.3.4.15'='Distribution of powers between institutions of the EU';
'1.3.5'='The subject of review';
'1.3.5.1'='International treaties';
'1.3.5.2'='Community law';
'1.3.5.2.1'='Primary legislation';
'1.3.5.2.2'='Secondary legislation';
'1.3.5.3'='Constitution';
'1.3.5.4'='Quasi-constitutional legislation';
'1.3.5.5'='Laws and other rules having the force of law';
'1.3.5.5.1'='Laws and other rules in force before the entry into force of the Constitution';
'1.3.5.6'='Decrees of the Head of State';
'1.3.5.7'='Quasi-legislative regulations';
'1.3.5.8'='Rules issued by federal or regional entities';
'1.3.5.9'='Parliamentary rules';
'1.3.5.10'='Rules issued by the executive';
'1.3.5.11'='Acts issued by decentralised bodies';
'1.3.5.11.1'='Territorial decentralisation';
'1.3.5.11.2'='Sectoral decentralisation';
'1.3.5.12'='Court decisions';
'1.3.5.13'='Administrative acts';
'1.3.5.14'='Government acts';
'1.3.5.15'='Failure to act or to pass legislation';
'1.4'='Procedure';
'1.4.1'='General characteristics';
'1.4.2'='Summary procedure';
'1.4.3'='Time-limits for instituting proceedings';
'1.4.3.1'='Ordinary time-limit';
'1.4.3.2'='Special time-limits';
'1.4.3.3'='Leave to appeal out of time';
'1.4.4'='Exhaustion of remedies';
'1.4.5'='Originating document';
'1.4.5.1'='Decision to act';
'1.4.5.2'='Signature';
'1.4.5.3'='Formal requirements';
'1.4.5.4'='Annexes';
'1.4.5.5'='Service';
'1.4.6'='Grounds';
'1.4.6.1'='Time-limits';
'1.4.6.2'='Form';
'1.4.6.3'='Ex-officio grounds';
'1.4.7'='Documents lodged by the parties';
'1.4.7.1'='Time-limits';
'1.4.7.2'='Decision to lodge the document';
'1.4.7.3'='Signature';
'1.4.7.4'='Formal requirements';
'1.4.7.5'='Annexes';
'1.4.7.6'='Service';
'1.4.8'='Preparation of the case for trial';
'1.4.8.1'='Registration';
'1.4.8.2'='Notifications and publication';
'1.4.8.3'='Time-limits';
'1.4.8.4'='Preliminary proceedings';
'1.4.8.5'='Opinions';
'1.4.8.6'='Reports';
'1.4.8.7'='Evidence';
'1.4.8.7.1'='Inquiries into the facts by the Court';
'1.4.8.8'='Decision that preparation is complete';
'1.4.9'='Parties';
'1.4.9.1'='Locus standi';
'1.4.9.2'='Interest';
'1.4.9.3'='Representation';
'1.4.9.3.1'='The Bar';
'1.4.9.3.2'='Legal representation other than the Bar';
'1.4.9.3.3'='Representation by persons other than lawyers or jurists';
'1.4.9.4'='Persons or entities authorised to intervene in proceedings';
'1.4.10'='Interlocutory proceedings';
'1.4.10.1'='Intervention';
'1.4.10.2'='Plea of forgery';
'1.4.10.3'='Resumption of proceedings after interruption';
'1.4.10.4'='Discontinuance of proceedings';
'1.4.10.5'='Joinder of similar cases';
'1.4.10.6'='Challenging of a judge';
'1.4.10.6.1'='Automatic disqualification';
'1.4.10.6.2'='Challenge at the instance of a party';
'1.4.10.7'='Request for a preliminary ruling by the Court of Justice of the EU';
'1.4.11'='Hearing';
'1.4.11.1'='Composition of the bench';
'1.4.11.2'='Procedure';
'1.4.11.3'='In public / in camera';
'1.4.11.4'='Report';
'1.4.11.5'='Opinion';
'1.4.11.6'='Address by the parties';
'1.4.12'='Special procedures';
'1.4.13'='Re-opening of hearing';
'1.4.14'='Costs';
'1.4.14.1'='Waiver of court fees';
'1.4.14.2'='Legal aid or assistance';
'1.4.14.3'='Party costs';
'1.5'='Decisions';
'1.5.1'='Deliberation';
'1.5.1.1'='Composition of the bench';
'1.5.1.2'='Chair';
'1.5.1.3'='Procedure';
'1.5.1.3.1'='Quorum';
'1.5.1.3.2'='Vote';
'1.5.2'='Reasoning';
'1.5.3'='Form';
'1.5.4'='Types';
'1.5.4.1'='Procedural decisions';
'1.5.4.2'='Opinion';
'1.5.4.3'='Finding of constitutionality or unconstitutionality';
'1.5.4.4'='Annulment';
'1.5.4.4.1'='Consequential annulment';
'1.5.4.5'='Suspension';
'1.5.4.6'='Modification';
'1.5.4.7'='Interim measures';
'1.5.5'='Individual opinions of members';
'1.5.5.1'='Concurring opinions';
'1.5.5.2'='Dissenting opinions';
'1.5.6'='Delivery and publication';
'1.5.6.1'='Delivery';
'1.5.6.2'='Time limit';
'1.5.6.3'='Publication';
'1.5.6.3.1'='Publication in the official journal/gazette';
'1.5.6.3.2'='Publication in an official collection';
'1.5.6.3.3'='Private publication';
'1.5.6.4'='Press';
'1.6'='Effects';
'1.6.1'='Scope';
'1.6.2'='Determination of effects by the court';
'1.6.3'='Effect erga omnes';
'1.6.3.1'='Stare decisis';
'1.6.4'='Effect inter partes';
'1.6.5'='Temporal effect';
'1.6.5.1'='Entry into force of decision';
'1.6.5.2'='Retrospective effect (ex tunc)';
'1.6.5.3'='Limitation on retrospective effect';
'1.6.5.4'='Ex nunc effect';
'1.6.5.5'='Postponement of temporal effect';
'1.6.6'='Execution';
'1.6.6.1'='Body responsible for supervising execution';
'1.6.6.2'='Penalty payment';
'1.6.7'='Influence on State organs';
'1.6.8'='Influence on everyday life';
'1.6.9'='Consequences for other cases';
'1.6.9.1'='Ongoing cases';
'1.6.9.2'='Decided cases';
'2'='Sources';
'2.1'='Categories';
'2.1.1'='Written rules';
'2.1.1.1'='National rules';
'2.1.1.1.1'='Constitution';
'2.1.1.1.2'='Quasi-constitutional enactments';
'2.1.1.2'='National rules from other countries';
'2.1.1.3'='Community law';
'2.1.1.4'='International instruments';
'2.1.1.4.1'='United Nations Charter of 1945';
'2.1.1.4.2'='Universal Declaration of Human Rights of 1948';
'2.1.1.4.3'='Geneva Conventions of 1949';
'2.1.1.4.4'='European Convention on Human Rights of 1950';
'2.1.1.4.5'='Geneva Convention on the Status of Refugees of 1951';
'2.1.1.4.6'='European Social Charter of 1961';
'2.1.1.4.7'='International Convention on the Elimination of all Forms of Racial Discrimination of 1965';
'2.1.1.4.8'='International Covenant on Civil and Political Rights of 1966';
'2.1.1.4.9'='International Covenant on Economic, Social and Cultural Rights of 1966';
'2.1.1.4.10'='Vienna Convention on the Law of Treaties of 1969';
'2.1.1.4.11'='American Convention on Human Rights of 1969';
'2.1.1.4.12'='Convention on the Elimination of all Forms of Discrimination against Women of 1979';
'2.1.1.4.13'='African Charter on Human and Peoples\\' Rights of 1981';
'2.1.1.4.14'='European Charter of Local Self-Government of 1985';
'2.1.1.4.15'='Convention on the Rights of the Child of 1989';
'2.1.1.4.16'='Framework Convention for the Protection of National Minorities of 1995';
'2.1.1.4.17'='Statute of the International Criminal Court of 1998';
'2.1.1.4.18'='Charter of Fundamental Rights of the European Union of 2000';
'2.1.1.4.19'='International conventions regulating diplomatic and consular relations';
'2.1.2'='Unwritten rules';
'2.1.2.1'='Constitutional custom';
'2.1.2.2'='General principles of law';
'2.1.2.3'='Natural law';
'2.1.3'='Case-law';
'2.1.3.1'='Domestic case-law';
'2.1.3.2'='International case-law';
'2.1.3.2.1'='European Court of Human Rights';
'2.1.3.2.2'='Court of Justice of the European Communities';
'2.1.3.2.3'='Other international bodies';
'2.1.3.3'='Foreign case-law';
'2.2'='Hierarchy';
'2.2.1'='Hierarchy as between national and non-national sources';
'2.2.1.1'='Treaties and constitutions';
'2.2.1.2'='Treaties and legislative acts';
'2.2.1.3'='Treaties and other domestic legal instruments';
'2.2.1.4'='European Convention on Human Rights and constitutions';
'2.2.1.5'='European Convention on Human Rights and non-constitutional domestic legal instruments';
'2.2.1.6'='Community law and domestic law';
'2.2.1.6.1'='Primary Community legislation and constitutions';
'2.2.1.6.2'='Primary Community legislation and domestic nonconstitutional legal instruments';
'2.2.1.6.3'='Secondary Community legislation and constitutions';
'2.2.1.6.4'='Secondary Community legislation and domestic nonconstitutional instruments';
'2.2.2'='Hierarchy as between national sources';
'2.2.2.1'='Hierarchy emerging from the Constitution';
'2.2.2.1.1'='Hierarchy attributed to rights and freedoms';
'2.2.2.2'='The Constitution and other sources of domestic law';
'2.2.3'='Hierarchy between sources of Community law';
'2.3'='Techniques of review';
'2.3.1'='Concept of manifest error in assessing evidence or exercising discretion';
'2.3.2'='Concept of constitutionality dependent on a specified interpretation';
'2.3.3'='Intention of the author of the enactment under review';
'2.3.4'='Interpretation by analogy';
'2.3.5'='Logical interpretation';
'2.3.6'='Historical interpretation';
'2.3.7'='Literal interpretation';
'2.3.8'='Systematic interpretation';
'2.3.9'='Teleological interpretation';
'2.3.10'='Contextual interpretation';
'2.3.11'='Pro homine/most favourable interpretation to the individual';
'3'='General Principles';
'3.1'='Sovereignty';
'3.2'='Republic/Monarchy';
'3.3'='Democracy';
'3.3.1'='Representative democracy';
'3.3.2'='Direct democracy';
'3.3.3'='Pluralist democracy';
'3.4'='Separation of powers';
'3.5'='Social State';
'3.6'='Structure of the State';
'3.6.1'='Unitary State';
'3.6.2'='Regional State';
'3.6.3'='Federal State';
'3.7'='Relations between the State and bodies of a religious or ideological nature';
'3.8'='Territorial principles';
'3.8.1'='Indivisibility of the territory';
'3.9'='Rule of law';
'3.10'='Certainty of the law';
'3.11'='Vested and/or acquired rights';
'3.12'='Clarity and precision of legal provisions';
'3.13'='Legality';
'3.14'='Nullum crimen, nulla poena sine lege';
'3.15'='Publication of laws';
'3.15.1'='Ignorance of the law is no excuse';
'3.15.2'='Linguistic aspects';
'3.16'='Proportionality';
'3.17'='Weighing of interests';
'3.18'='General interest';
'3.19'='Margin of appreciation';
'3.20'='Reasonableness';
'3.21'='Equality';
'3.22'='Prohibition of arbitrariness';
'3.23'='Equity';
'3.24'='Loyalty to the State';
'3.25'='Market economy';
'3.26'='Principles of EU law';
'3.26.1'='Fundamental principles of the Common Market';
'3.26.2'='Direct effect';
'3.26.3'='Genuine co-operation between the institutions and the member states';
'4'='Institutions';
'4.1'='Constituent assembly or equivalent body';
'4.1.1'='Procedure';
'4.1.2'='Limitations on powers';
'4.2'='State Symbols';
'4.2.1'='Flag';
'4.2.2'='National holiday';
'4.2.3'='National anthem';
'4.2.4'='National emblem';
'4.2.5'='Motto';
'4.2.6'='Capital city';
'4.3'='Languages';
'4.3.1'='Official language(s)';
'4.3.2'='National language(s)';
'4.3.3'='Regional language(s)';
'4.3.4'='Minority language(s)';
'4.4'='Head of State';
'4.4.1'='Vice-President / Regent';
'4.4.2'='Temporary replacement';
'4.4.3'='Powers';
'4.4.3.1'='Relations with legislative bodies';
'4.4.3.2'='Relations with the executive bodies';
'4.4.3.3'='Relations with judicial bodies';
'4.4.3.4'='Promulgation of laws';
'4.4.3.5'='International relations';
'4.4.3.6'='Powers with respect to the armed forces';
'4.4.3.7'='Mediating powers';
'4.4.4'='Appointment';
'4.4.4.1'='Necessary qualifications';
'4.4.4.2'='Incompatibilities';
'4.4.4.3'='Direct/indirect election';
'4.4.4.4'='Hereditary succession';
'4.4.5'='Term of office';
'4.4.5.1'='Commencement of office';
'4.4.5.2'='Duration of office';
'4.4.5.3'='Incapacity';
'4.4.5.4'='End of office';
'4.4.5.5'='Limit on number of successive terms';
'4.4.6'='Status';
'4.4.6.1'='Liability';
'4.4.6.1.1'='Legal liability';
'4.4.6.1.1.1'='Immunity';
'4.4.6.1.1.2'='Civil liability';
'4.4.6.1.1.3'='Criminal liability';
'4.4.6.1.2'='Political responsibility';
'4.5'='Legislative bodies';
'4.5.1'='Structure';
'4.5.2'='Powers';
'4.5.2.1'='Competences with respect to international agreements';
'4.5.2.2'='Powers of enquiry';
'4.5.2.3'='Delegation to another legislative body';
'4.5.2.4'='Negative incompetence';
'4.5.3'='Composition';
'4.5.3.1'='Election of members';
'4.5.3.2'='Appointment of members';
'4.5.3.3'='Term of office of the legislative body';
'4.5.3.3.1'='Duration';
'4.5.3.4'='Term of office of members';
'4.5.3.4.1'='Characteristics';
'4.5.3.4.2'='Duration';
'4.5.3.4.3'='End';
'4.5.4'='Organisation';
'4.5.4.1'='Rules of procedure';
'4.5.4.2'='President/Speaker';
'4.5.4.3'='Sessions';
'4.5.4.4'='Committees';
'4.5.4.5'='Parliamentary groups';
'4.5.5'='Finances';
'4.5.6'='Law-making procedure';
'4.5.6.1'='Right to initiate legislation';
'4.5.6.2'='Quorum';
'4.5.6.3'='Majority required';
'4.5.6.4'='Right of amendment';
'4.5.6.5'='Relations between houses';
'4.5.7'='Relations with the executive bodies';
'4.5.7.1'='Questions to the government';
'4.5.7.2'='Questions of confidence';
'4.5.7.3'='Motion of censure';
'4.5.8'='Relations with judicial bodies';
'4.5.9'='Liability';
'4.5.10'='Political parties';
'4.5.10.1'='Creation';
'4.5.10.2'='Financing';
'4.5.10.3'='Role';
'4.5.10.4'='Prohibition';
'4.5.11'='Status of members of legislative bodies';
'4.6'='Executive bodies';
'4.6.1'='Hierarchy';
'4.6.2'='Powers';
'4.6.3'='Application of laws';
'4.6.3.1'='Autonomous rule-making powers';
'4.6.3.2'='Delegated rule-making powers';
'4.6.4'='Composition';
'4.6.4.1'='Appointment of members';
'4.6.4.2'='Election of members';
'4.6.4.3'='End of office of members';
'4.6.4.4'='Status of members of executive bodies';
'4.6.5'='Organisation';
'4.6.6'='Relations with judicial bodies';
'4.6.7'='Administrative decentralisation';
'4.6.8'='Sectoral decentralisation';
'4.6.8.1'='Universities';
'4.6.9'='The civil service';
'4.6.9.1'='Conditions of access';
'4.6.9.2'='Reasons for exclusion';
'4.6.9.2.1'='Lustration';
'4.6.9.3'='Remuneration';
'4.6.9.4'='Personal liability';
'4.6.9.5'='Trade union status';
'4.6.10'='Liability';
'4.6.10.1'='Legal liability';
'4.6.10.1.1'='Immunity';
'4.6.10.1.2'='Civil liability';
'4.6.10.1.3'='Criminal liability';
'4.6.10.2'='Political responsibility';
'4.7'='Judicial bodies';
'4.7.1'='Jurisdiction';
'4.7.1.1'='Exclusive jurisdiction';
'4.7.1.2'='Universal jurisdiction';
'4.7.1.3'='Conflicts of jurisdiction';
'4.7.2'='Procedure';
'4.7.3'='Decisions';
'4.7.4'='Organisation';
'4.7.4.1'='Members';
'4.7.4.1.1'='Qualifications';
'4.7.4.1.2'='Appointment';
'4.7.4.1.3'='Election';
'4.7.4.1.4'='Term of office';
'4.7.4.1.5'='End of office';
'4.7.4.1.6'='Status';
'4.7.4.1.6.1'='Incompatibilities';
'4.7.4.1.6.2'='Discipline';
'4.7.4.1.6.3'='Irremovability';
'4.7.4.2'='Officers of the court';
'4.7.4.3'='Prosecutors / State counsel';
'4.7.4.3.1'='Powers';
'4.7.4.3.2'='Appointment';
'4.7.4.3.3'='Election';
'4.7.4.3.4'='Term of office';
'4.7.4.3.5'='End of office';
'4.7.4.3.6'='Status';
'4.7.4.4'='Languages';
'4.7.4.5'='Registry';
'4.7.4.6'='Budget';
'4.7.5'='Supreme Judicial Council or equivalent body';
'4.7.6'='Relations with bodies of international jurisdiction';
'4.7.7'='Supreme court';
'4.7.8'='Ordinary courts';
'4.7.8.1'='Civil courts';
'4.7.8.2'='Criminal courts';
'4.7.9'='Administrative courts';
'4.7.10'='Financial courts';
'4.7.11'='Military courts';
'4.7.12'='Special courts';
'4.7.13'='Other courts';
'4.7.14'='Arbitration';
'4.7.15'='Legal assistance and representation of parties';
'4.7.15.1'='The Bar';
'4.7.15.1.1'='Organisation';
'4.7.15.1.2'='Powers of ruling bodies';
'4.7.15.1.3'='Role of members of the Bar';
'4.7.15.1.4'='Status of members of the Bar';
'4.7.15.1.5'='Discipline';
'4.7.15.2'='Assistance other than by the Bar';
'4.7.15.2.1'='Legal advisers';
'4.7.15.2.2'='Legal assistance bodies';
'4.7.16'='Liability';
'4.7.16.1'='Liability of the State';
'4.7.16.2'='Liability of judges';
'4.8'='Federalism, regionalism and local self-government';
'4.8.1'='Federal entities';
'4.8.2'='Regions and provinces';
'4.8.3'='Municipalities';
'4.8.4'='Basic principles';
'4.8.4.1'='Autonomy';
'4.8.4.2'='Subsidiarity';
'4.8.5'='Definition of geographical boundaries';
'4.8.6'='Institutional aspects';
'4.8.6.1'='Deliberative assembly';
'4.8.6.1.1'='Status of members';
'4.8.6.2'='Executive';
'4.8.6.3'='Courts';
'4.8.7'='Budgetary and financial aspects';
'4.8.7.1'='Finance';
'4.8.7.2'='Arrangements for distributing the financial resources of the State';
'4.8.7.3'='Budget';
'4.8.7.4'='Mutual support arrangements';
'4.8.8'='Distribution of powers';
'4.8.8.1'='Principles and methods';
'4.8.8.2'='Implementation';
'4.8.8.2.1'='Distribution ratione materiae';
'4.8.8.2.2'='Distribution ratione loci';
'4.8.8.2.3'='Distribution ratione temporis';
'4.8.8.2.4'='Distribution ratione personae';
'4.8.8.3'='Supervision';
'4.8.8.4'='Co-operation';
'4.8.8.5'='International relations';
'4.8.8.5.1'='Conclusion of treaties';
'4.8.8.5.2'='Participation in international organisations or their organs';
'4.9'='Elections and instruments of direct democracy';
'4.9.1'='Competent body for the organisation and control of voting';
'4.9.2'='Referenda and other instruments of direct democracy';
'4.9.2.1'='Admissibility';
'4.9.2.2'='Effects';
'4.9.3'='Electoral system';
'4.9.3.1'='Method of voting';
'4.9.4'='Constituencies';
'4.9.5'='Eligibility';
'4.9.6'='Representation of minorities';
'4.9.7'='Preliminary procedures';
'4.9.7.1'='Electoral rolls';
'4.9.7.2'='Registration of parties and candidates';
'4.9.7.3'='Ballot papers';
'4.9.8'='Electoral campaign and campaign material';
'4.9.8.1'='Campaign financing';
'4.9.8.2'='Campaign expenses';
'4.9.8.3'='Access to media';
'4.9.9'='Voting procedures';
'4.9.9.1'='Polling stations';
'4.9.9.2'='Polling booths';
'4.9.9.3'='Voting';
'4.9.9.4'='Identity checks on voters';
'4.9.9.5'='Record of persons having voted';
'4.9.9.6'='Casting of votes';
'4.9.10'='Minimum participation rate required';
'4.9.11'='Determination of votes';
'4.9.11.1'='Counting of votes';
'4.9.11.2'='Electoral reports';
'4.9.12'='Proclamation of results';
'4.9.13'='Post-electoral procedures';
'4.1'='Public finances';
'4.10.1'='Principles';
'4.10.2'='Budget';
'4.10.3'='Accounts';
'4.10.4'='Currency';
'4.10.5'='Central bank';
'4.10.6'='Auditing bodies';
'4.10.7'='Taxation';
'4.10.7.1'='Principles';
'4.10.8'='Public assets';
'4.10.8.1'='Privatisation';
'4.11'='Armed forces, police forces and secret services';
'4.11.1'='Armed forces';
'4.11.2'='Police forces';
'4.11.3'='Secret services';
'4.12'='Ombudsman';
'4.12.1'='Appointment';
'4.12.2'='Guarantees of independence';
'4.12.2.1'='Term of office';
'4.12.2.2'='Incompatibilities';
'4.12.2.3'='Immunities';
'4.12.2.4'='Financial independence';
'4.12.3'='Powers';
'4.12.4'='Organisation';
'4.12.5'='Relations with the Head of State';
'4.12.6'='Relations with the legislature';
'4.12.7'='Relations with the executive';
'4.12.8'='Relations with auditing bodies';
'4.12.9'='Relations with judicial bodies';
'4.12.10'='Relations with federal or regional authorities';
'4.13'='Independent administrative authorities';
'4.14'='Activities and duties assigned to the State by the Constitution';
'4.15'='Exercise of public functions by private bodies';
'4.16'='International relations';
'4.16.1'='Transfer of powers to international institutions';
'4.17'='European Union';
'4.17.1'='Institutional structure';
'4.17.1.1'='European Parliament';
'4.17.1.2'='Council';
'4.17.1.3'='Commission';
'4.17.1.4'='Court of Justice of the EU';
'4.17.2'='Distribution of powers between the EU and member states';
'4.17.3'='Distribution of powers between institutions of the EU';
'4.17.4'='Legislative procedure';
'4.18'='State of emergency and emergency powers';
'5'='Fundamental Rights';
'5.1'='General questions';
'5.1.1'='Entitlement to rights';
'5.1.1.1'='Nationals';
'5.1.1.1.1'='Nationals living abroad';
'5.1.1.2'='Citizens of the European Union and non-citizens with similar status';
'5.1.1.3'='Foreigners';
'5.1.1.3.1'='Refugees and applicants for refugee status';
'5.1.1.4'='Natural persons';
'5.1.1.4.1'='Minors';
'5.1.1.4.2'='Incapacitated';
'5.1.1.4.3'='Detainees';
'5.1.1.4.4'='Military personnel';
'5.1.1.5'='Legal persons';
'5.1.1.5.1'='Private law';
'5.1.1.5.2'='Public law';
'5.1.2'='Horizontal effects';
'5.1.3'='Positive obligation of the state';
'5.1.4'='Limits and restrictions';
'5.1.4.1'='Non-derogable rights';
'5.1.4.2'='General/special clause of limitation';
'5.1.4.3'='Subsequent review of limitation';
'5.1.5'='Emergency situations';
'5.2'='Equality';
'5.2.1'='Scope of application';
'5.2.1.1'='Public burdens';
'5.2.1.2'='Employment';
'5.2.1.2.1'='In private law';
'5.2.1.2.2'='In public law';
'5.2.1.3'='Social security';
'5.2.1.4'='Elections';
'5.2.2'='Criteria of distinction';
'5.2.2.1'='Gender';
'5.2.2.2'='Race';
'5.2.2.3'='Ethnic origin';
'5.2.2.4'='Citizenship or nationality';
'5.2.2.5'='Social origin';
'5.2.2.6'='Religion';
'5.2.2.7'='Age';
'5.2.2.8'='Physical or mental disability';
'5.2.2.9'='Political opinions or affiliation';
'5.2.2.10'='Language';
'5.2.2.11'='Sexual orientation';
'5.2.2.12'='Civil status';
'5.2.2.13'='Differentiation ratione temporis';
'5.2.3'='Affirmative action';
'5.3'='Civil and political rights';
'5.3.1'='Right to dignity';
'5.3.2'='Right to life';
'5.3.3'='Prohibition of torture and inhuman and degrading treatment';
'5.3.4'='Right to physical and psychological integrity';
'5.3.4.1'='Scientific and medical treatment and experiments';
'5.3.5'='Individual liberty';
'5.3.5.1'='Deprivation of liberty';
'5.3.5.1.1'='Arrest';
'5.3.5.1.2'='Non-penal measures';
'5.3.5.1.3'='Detention pending trial';
'5.3.5.1.4'='Conditional release';
'5.3.5.2'='Prohibition of forced or compulsory labour';
'5.3.6'='Freedom of movement';
'5.3.7'='Right to emigrate';
'5.3.8'='Right to citizenship or nationality';
'5.3.9'='Right of residence';
'5.3.10'='Rights of domicile and establishment';
'5.3.11'='Right of asylum';
'5.3.12'='Security of the person';
'5.3.13'='Procedural safeguards, rights of the defence and fair trial';
'5.3.13.1'='Scope';
'5.3.13.1.1'='Constitutional proceedings';
'5.3.13.1.2'='Civil proceedings';
'5.3.13.1.3'='Criminal proceedings';
'5.3.13.1.4'='Litigious administrative proceedings';
'5.3.13.1.5'='Non-litigious administrative proceedings';
'5.3.13.2'='Effective remedy';
'5.3.13.3'='Access to courts';
'5.3.13.3.1'='“Natural judge”/Tribunal established by law';
'5.3.13.3.2'='Habeas corpus';
'5.3.13.4'='Double degree of jurisdiction';
'5.3.13.5'='Suspensive effect of appeal';
'5.3.13.6'='Right to a hearing';
'5.3.13.7'='Right to participate in the administration of justice';
'5.3.13.8'='Right of access to the file';
'5.3.13.9'='Public hearings';
'5.3.13.10'='Trial by jury';
'5.3.13.11'='Public judgments';
'5.3.13.12'='Right to be informed about the decision';
'5.3.13.13'='Trial/decision within reasonable time';
'5.3.13.14'='Independence';
'5.3.13.15'='Impartiality';
'5.3.13.16'='Prohibition of reformatio in peius';
'5.3.13.17'='Rules of evidence';
'5.3.13.18'='Reasoning';
'5.3.13.19'='Equality of arms';
'5.3.13.20'='Adversarial principle';
'5.3.13.21'='Languages';
'5.3.13.22'='Presumption of innocence';
'5.3.13.23'='Right to remain silent';
'5.3.13.23.1'='Right not to incriminate oneself';
'5.3.13.23.2'='Right not to testify against spouse/close family';
'5.3.13.24'='Right to be informed about the reasons of detention';
'5.3.13.25'='Right to be informed about the charges';
'5.3.13.26'='Right to have adequate time and facilities for the preparation of the case';
'5.3.13.27'='Right to counsel';
'5.3.13.27.1'='Right to paid legal assistance';
'5.3.13.28'='Right to examine witnesses';
'5.3.14'='Ne bis in idem';
'5.3.15'='Rights of victims of crime';
'5.3.16'='Principle of the application of the more lenient law';
'5.3.17'='Right to compensation for damage caused by the State';
'5.3.18'='Freedom of conscience';
'5.3.19'='Freedom of opinion';
'5.3.20'='Freedom of worship';
'5.3.21'='Freedom of expression';
'5.3.22'='Freedom of the written press';
'5.3.23'='Rights in respect of the audiovisual media and other means of mass communication';
'5.3.24'='Right to information';
'5.3.25'='Right to administrative transparency';
'5.3.25.1'='Right of access to administrative documents';
'5.3.26'='National service';
'5.3.27'='Freedom of association';
'5.3.28'='Freedom of assembly';
'5.3.29'='Right to participate in public affairs';
'5.3.29.1'='Right to participate in political activity';
'5.3.30'='Right of resistance';
'5.3.31'='Right to respect for one\\'s honour and reputation';
'5.3.32'='Right to private life';
'5.3.32.1'='Protection of personal data';
'5.3.33'='Right to family life';
'5.3.33.1'='Descent';
'5.3.33.2'='Succession';
'5.3.34'='Right to marriage';
'5.3.35'='Inviolability of the home';
'5.3.36'='Inviolability of communications';
'5.3.36.1'='Correspondence';
'5.3.36.2'='Telephonic communications';
'5.3.36.3'='Electronic communications';
'5.3.37'='Right of petition';
'5.3.38'='Non-retrospective effect of law';
'5.3.38.1'='Criminal law';
'5.3.38.2'='Civil law';
'5.3.38.3'='Social law';
'5.3.38.4'='Taxation law';
'5.3.39'='Right to property';
'5.3.39.1'='Expropriation';
'5.3.39.2'='Nationalisation';
'5.3.39.3'='Other limitations';
'5.3.39.4'='Privatisation';
'5.3.40'='Linguistic freedom';
'5.3.41'='Electoral rights';
'5.3.41.1'='Right to vote';
'5.3.41.2'='Right to stand for election';
'5.3.41.3'='Freedom of voting';
'5.3.41.4'='Secret ballot';
'5.3.41.5'='Direct / indirect ballot';
'5.3.41.6'='Frequency and regularity of elections';
'5.3.42'='Rights in respect of taxation';
'5.3.43'='Right to self fulfilment';
'5.3.44'='Rights of the child';
'5.3.45'='Protection of minorities and persons belonging to minorities';
'5.4'='Economic, social and cultural rights';
'5.4.1'='Freedom to teach';
'5.4.2'='Right to education';
'5.4.3'='Right to work';
'5.4.4'='Freedom to choose one\\'s profession';
'5.4.5'='Freedom to work for remuneration';
'5.4.6'='Commercial and industrial freedom';
'5.4.7'='Consumer protection';
'5.4.8'='Freedom of contract';
'5.4.9'='Right of access to the public service';
'5.4.10'='Right to strike';
'5.4.11'='Freedom of trade unions';
'5.4.12'='Right to intellectual property';
'5.4.13'='Right to housing';
'5.4.14'='Right to social security';
'5.4.15'='Right to unemployment benefits';
'5.4.16'='Right to a pension';
'5.4.17'='Right to just and decent working conditions';
'5.4.18'='Right to a sufficient standard of living';
'5.4.19'='Right to health';
'5.4.20'='Right to culture';
'5.4.21'='Scientific freedom';
'5.4.22'='Artistic freedom';
'5.5'='Collective rights';
'5.5.1'='Right to the environment';
'5.5.2'='Right to development';
'5.5.3'='Right to peace';
'5.5.4'='Right to self-determination';
'5.5.5'='Rights of aboriginal peoples, ancestral rights'"


plaintiff.string<-"'10'='gender not ascertained';
	'11'='male';
	'12'='female';
	'13'='male child';
	'14'='female child';
	'15'='child, gender not ascertained';
	'20'='obviously small, individual business';
	'21'='other business';
	'30'='unable to classify specific type';
	'31'='business or trade association, professional association,';
	'32'='union (or associations of teachers or workers)';
	'33'='educational association or institution';
	'34'='religious or charitable organization';
	'35'='non-party political group (e.g., Greenpeace, Amnesty International, etc.,)';
	'36'='tribe or association of indigenous people';
	'37'='other';
	'40'='city or other local mayor';
	'41'='city or other local executive (collective)';
	'42'='city or other local assembly/parliament';
	'43'='city or other local department (e.g., department of social services, department of education)';
	'44'='city or other local legal official, inc. prosecutor';
	'45'='party represented in city or other local assembly/parliament';
	'50'='regional or provincial mayor/governor';
	'51'='regional or provincial executive (collective)';
	'52'='regional or provincial assembly/parliament';
	'53'='regional or provincial department (e.g., department of social services, department of education)';
	'54'='regional or provincial legal official, inc. prosecutor';
	'55'='party represented in regional or provincial assembly/parliament';
	'60'='national head of government (prime minister, president of the council of ministers)';
	'61'='national head of state (president)';
	'62'='national government minister';
	'63'='national justice ministry, attorney general, Lord Chancellor';
	'64'='national - corporate capacity in criminal prosecution (e.g., the Queen or the People)';
	'65'='national - corporate capacity in civil case';
	'66'='national - judiciary';
	'66'='national - central bank';
	'67'='national - ombudsman (inc. Chancellor of Justice)';
	'68'='national - independent regulatory body';
	'70'='national parliament as corporate body';
	'71'='speaker/chairman of national parliament (either house)';
	'72'='political party represented in national parliament, part of governing coalition';
	'73'='political party represented in national parliament, not part of governing coalition';
	'74'='individual member of parliament, part of governing coalition';
	'75'='individual member of parliament, not part of governing coalition';
	'79'='individual members of parliament or groups of members of parliament, parties unclear';
	'90'='foreign government';
	'91'='international organization (including EU)';
	'92'='court appointed official (trustees, executors, fiduciaries, liquidators)';
	'93'='tribal authority, independent homelands authority';
	'94'='other';
	'95'='not able to classify'"

plaintiff.string.general<-"'1'='natural person';'2'='private business';'3'='association';c('4','5')='subnational government';c('6','7')='national government';'9'='other'"

dat <- import("estonia_manual_vote_matrix.xls", )
judge.pos<-4:31


###################################################
### code chunk number 2: recodes
###################################################
for (j in judge.pos) {
	dat[,j] <- as.numeric(dat[,j])
}

dat$legal.area.1.rc<-as.character(car:::recode(dat$legal.area.1,venice.string))
dat$legal.area.2.rc<-as.character(car:::recode(dat$legal.area.2,venice.string))
dat$legal.area.3.rc<-as.character(car:::recode(dat$legal.area3,venice.string))

dat$Plaintiff.1.rc<-car:::recode(dat$Plaintiff.1,plaintiff.string)
dat$Plaintiff.2.rc<-car:::recode(dat$Plaintiff.2,plaintiff.string)
dat$Plaintiff.3.rc<-car:::recode(dat$Plaintiff.3,plaintiff.string)

dat$Plaintiff.1.general<-car:::recode(substr(dat$Plaintiff.1,1,1),plaintiff.string.general)
dat$Plaintiff.2.general<-car:::recode(substr(dat$Plaintiff.2,1,1),plaintiff.string.general)
dat$Plaintiff.3.general<-car:::recode(substr(dat$Plaintiff.2,1,1),plaintiff.string.general)

dat$Respondent.1.general<-car:::recode(substr(dat$Respondent.1,1,1),plaintiff.string.general)
dat$Respondent.2.general<-car:::recode(substr(dat$Respondent.2,1,1),plaintiff.string.general)
dat$Respondent.3.general<-car:::recode(substr(dat$Respondent.3,1,1),plaintiff.string.general)

dat$Respondent.1.rc<-car:::recode(dat$Respondent.1,plaintiff.string)
dat$Respondent.2.rc<-car:::recode(dat$Respondent.2,plaintiff.string)
dat$Respondent.3.rc<-car:::recode(dat$Respondent.3,plaintiff.string)

dat$minvotes<-apply(dat[,judge.pos],1,function(x)sum(x==0,na.rm=T))
dat$majvotes<-apply(dat[,judge.pos],1,function(x)sum(x==1,na.rm=T))

common.participants<-unlist(dat[,c("Plaintiff.1.rc","Plaintiff.2.rc","Plaintiff.3.rc",
	"Respondent.1.rc","Respondent.2.rc","Respondent.3.rc")])


###################################################
### code chunk number 3: staton
###################################################
staton <- read.csv("./jimeasures_full.csv",header=T)
staton$iso3c <- countrycode(staton$ccode,"cown","iso3c")
relevant.years <- 1994:max(staton$year[which(staton$iso3c=="EST")])
staton <- subset(staton,year %in% relevant.years)
## restrict to EU25 + USA

eu25plus1 <- c("AUT","BEL","BGR","CZE","CYP","DEU","DNK",
	"ESP","EST","FIN","FRA","GBR","GRC","HUN","IRL","ITA",
	"LVA","LTU","NLD","POL","ROU","SVK","SVN","LUX",
	"MLT","SWE","USA")
staton <- subset(staton,iso3c %in% eu25plus1)
staton$tatekeith <- NULL
rank.df <- aggregate(staton[,c("feldvoigt_dejure","feldvoigt_defacto","howardcarey","keith")],list(iso3c=staton$iso3c),mean,na.rm=T)

rank.df[,2:5] <- apply(rank.df[,2:5]*-1,2,rank,ties.method="min")



###################################################
### code chunk number 4: dissentsperyear
###################################################
est.years<-1993:2010
est.cases<-c(13,284,349,414,372,361,320, ## up to 1999
	368, 418,401,426,414,469,
	397,388,347,469,392)
est.levels<-data.frame(Year=est.years,Cases=est.cases)
est.levels2<-as.data.frame(table(substr(dat$Decdate,0,4)))
names(est.levels2)<-c("Year","Dissents")
est.levels<-merge(est.levels,est.levels2,all.x=T)
est.levels$Dissents[is.na(est.levels$Dissents)]<-0
est.levels$Ratio<-est.levels$Dissents/est.levels$Cases * 100
rm(est.levels2)
#plot(est.levels$Year,est.levels$Ratio,type="b",axes=F,xlab="Year",ylab="",pch=15,lty=2)
#par(new=TRUE)
#plot(est.levels$Year,est.levels$Dissents,type="b",axes=F,xlab="Year",ylab="",pch=19,lty=1)
#axis(1)
#axis(2)



###################################################
### code chunk number 5: irt
###################################################
set.seed(1982)
vote.mat<-dat[,judge.pos]
vote.mat<-as.matrix(vote.mat)
vote.mat<-t(vote.mat)

## Remove constant items
keep.list<-apply(vote.mat,2,var,na.rm=T)>0
vote.mat<-vote.mat[,which(keep.list==TRUE)]
## Must at the same time to the same time to dat
dat<-dat[which(keep.list==TRUE),]

rownames(vote.mat)<-names(dat)[judge.pos]

my.burnin<-.25e6
my.iter<-1.75e6
my.thin<-250

my.rc<-rollcall(vote.mat,legis.names=rownames(vote.mat))
my.ideal<-ideal(my.rc,d=1,
	normalize=TRUE,
	store.item=TRUE,
	maxiter=eval(my.iter),
	burnin=.25e6,
	thin=eval(my.thin))

my.ideal.random<-ideal(my.rc,d=1,
	normalize=TRUE,
	store.item=TRUE,
	startvals="random",
	maxiter=eval(my.iter),
	burnin=.25e6,
	thin=eval(my.thin))


my.irt<-idealToMCMC(my.ideal)
my.varnames<-c(paste("theta.",rownames(vote.mat),sep=""),paste("a.",1:ncol(vote.mat),sep=""),paste("b.",1:ncol(vote.mat),sep=""))
colnames(my.irt)<-my.varnames


###################################################
### code chunk number 6: irtdiags
###################################################
holder<-summary(my.irt)
my.preds<-predict(my.ideal)

## logLik
predprob<-my.preds$pred.probs
pijt_1<-log(predprob)
pijt_0<-log(1-predprob)

pijt_1<-pijt_1*(vote.mat==1)
pijt_0<-pijt_0*(vote.mat==0)

my.logLik<-sum(pijt_0,pijt_1,na.rm=T)

my.A<-length(which(!is.na(vote.mat)))

GMP<-exp(my.logLik/my.A)

try(gewekes<-geweke.diag(my.irt[,grep("theta",colnames(my.irt))])$z)
if (class(gewekes)=="try-error") {
	gewekes<-rnorm(25)*100
}



###################################################
### code chunk number 7: idealplot
###################################################
## Read in judge information
judges<-read.csv("./judge_details.csv",header=T)
judges$MatchString<-paste(judges$FirstName,judges$LastName,sep=" ")
judges$MatchDate<-paste(
	ifelse(is.na(judges$ApptDD),"01",sprintf("%02d",judges$ApptDD)),
	ifelse(is.na(judges$ApptMM),"01",sprintf("%02d",judges$ApptMM)),
	judges$ApptYYYY,sep="-")
judges$MatchDate<-as.Date(judges$MatchDate,"%d-%m-%Y")

judges$medleg<-NA

## Get Left-Right scores from auxiliary information
aux<-read.csv("./merged_party_positions.csv")
aux<-aux[aux$country_name_short=="EST",]
aux$edate<-as.Date(aux$edate)

for (i in 1:nrow(judges)) {
	the.date<-judges$MatchDate[i]
	aux$tmpdiff<-the.date - aux$edate 
	my.tmp<-aux[which(aux$tmpdiff>0),] ## should be positive -- appointment after election
	the.match<-which.min(my.tmp$tmpdiff)
	judges$medleg[i]<-my.tmp$medleg[the.match]
}

## Plot the positions of judges
results.df<-data.frame(holder$quantiles[grep("theta",rownames(holder$quantiles)),])
names(results.df)<-c("Lo","X5","Median","X95","Hi")
results.df$MatchString<-gsub("theta.","",rownames(results.df))
results.df$MatchString<-gsub("\\."," ",results.df$MatchString)
results.df$MatchString<-as.character(results.df$MatchString)
results.df$MatchString[results.df$MatchString=="Hele Kai Remmel"]<-"Hele-Kai Remmel"
results.df<-merge(results.df,judges,all.x=T)

results.df<-results.df[order(results.df$Median),]
results.df$LastName<-factor(results.df$LastName,levels=results.df$LastName,ordered=TRUE)

ideal.plot<-ggplot(data=results.df,aes(x=LastName,y=Median,ymax=Hi,ymin=Lo,color=Chamber,shape=Chamber)) + geom_pointrange() + theme_bw() + scale_y_continuous("Position") + scale_x_discrete("Judge") + coord_flip() + opts(legend.position="bottom")
print(ideal.plot)

results.df$ApptName2<-as.character(results.df$ApptName)
results.df$ApptName2[results.df$ApptName=="Arnold Rüütel"]<-"Rask"
results.df$ApptName2[results.df$ApptName=="Lennart Meri"]<-"Lõhmus"
#boxplot(results.df$Median~results.df$ApptName2)
#boxplot(results.df$Median~results.df$Chamber)


###################################################
### code chunk number 8: apptpos
###################################################
## Get Left-Right scores from auxiliary information
aux<-read.csv("./merged_party_positions.csv")
aux<-aux[aux$country_name_short=="EST",]
aux$edate<-as.Date(aux$edate)
results.df$ApptPos<-NA
for (i in 1:nrow(results.df)) {
	the.date<-results.df$MatchDate[i]
	aux$tmpdiff<-the.date - aux$edate 
	my.tmp<-aux[which(aux$tmpdiff>0),] ## should be positive -- appointment after election
	the.match<-which.min(my.tmp$tmpdiff)
	results.df$ApptPos[i]<-my.tmp$medleg[the.match]
}

results.df$ApptPos2 <- NA
results.df$ApptPos2[results.df$ApptName %in% c("Lennart Meri","Maruste","Lõhmus")] <- 7.6 ## Lennart Meri, so Pro Patria
results.df$ApptPos2[results.df$ApptName %in% c("Rask","Arnold Rüütel")] <- 4.0 ## Arnold Rüütel, so Kesk

t.test(results.df$Median~results.df$ApptPos2)
cor.test(results.df$Median,results.df$ApptPos2)


###################################################
### code chunk number 9: liberalcoding
###################################################
dat<-dat[keep.list,]
my.discriminating.cases<-my.ideal$beta[,,1]
my.discriminating.cases<-apply(my.discriminating.cases,2,function(x) {
	my.quantiles<-quantile(x,probs=c(0.025,0.975))
	my.quantiles[1]>0 | my.quantiles[2]<0
})
dat$discriminates<-my.discriminating.cases
dat$minvotes<-apply(dat[,judge.pos],1,function(x)sum(x==0,na.rm=T))


## Establish which side had majority
xbar<-holder$quantiles[grep("theta",rownames(holder$quantiles)),3]

dat$handedness <- apply(dat[,judge.pos],1,function(x) {
	majority <- xbar[which(x==1)]
	minority <- xbar[which(x==0)]
	ifelse((mean(majority,na.rm=T)>mean(minority,na.rm=T)),
		"RHS",
		"LHS")
})


casedescr <- read.csv("./case_areas.txt",sep="\t",header=F)
names(casedescr)<-c("legal.area.1.rc","direction")
dat <- merge(dat,casedescr,all.x=T,all.y=F)

for (i in which(is.na(dat$direction))) {
	area <- dat[i,"legal.area.2.rc"]
	direction <- unique(casedescr$direction[which(casedescr$legal.area.1.rc==area)])
	dat$direction[i] <- direction
}
for (i in which(is.na(dat$direction))) {
	area <- dat[i,"legal.area.3.rc"]
	direction <- unique(casedescr$direction[which(casedescr$legal.area.1.rc==area)])
}
## If it's 1, and the plaintiff wins, it's liberal
## if it's 1, and the respondent wins, it's conservative
## if it's 0, and the plaintiff wins, it's conservative
## if it's 0, and the respondent wins, it's liberal
dat$liberal <- NA
dat$liberal[which(dat$direction==1 & dat$winner==3)] <- 1
dat$liberal[which(dat$direction==0 & dat$winner==3)] <- 0
dat$liberal[which(dat$direction==0 & dat$winner==1)] <- 1
dat$liberal[which(dat$direction==1 & dat$winner==1)] <- 0
dat$liberal <- car:::recode(dat$liberal,
	"1='Liberal';0='Conservative'")
liberal.tab <- table(dat$liberal,dat$handedness)



###################################################
### code chunk number 10: liberaltab
###################################################
toLatex(liberal.tab)


###################################################
### code chunk number 11: discrimplot
###################################################
plaintiff.friendly<-table(dat$winner,dat$handedness)
dimnames(plaintiff.friendly)[[1]]<-c("Respondent","Neither","Plaintiff")
toLatex(plaintiff.friendly)


###################################################
### code chunk number 12: liberaloutcomes
###################################################
cat(as.character(casedescr$legal.area.1.rc[which(casedescr$direction==1)]),sep=", ")


###################################################
### code chunk number 13: conservativeoutcomes
###################################################
cat(as.character(casedescr$legal.area.1.rc[which(casedescr$direction==0)]),sep=", ")


###################################################
### code chunk number 14: neutraloutcomes
###################################################
cat(as.character(casedescr$legal.area.1.rc[which(is.na(casedescr$direction))]),sep=", ")


